﻿using UnityEngine;
using System.Collections.Generic;
using LitJson;

//本地玩家输入
public class LocalController : MonoBehaviour
{
    //public GameManager firePoint;
    public Transform firePoint;
    protected Player localPlayer;
    float press;
    public void SetLocol(Player value)
    {
        localPlayer = value;
        value.gameObject.transform.GetChild(4).gameObject.SetActive(true);
    }

    void Update()
    {
        if (localPlayer)
        {
            if (Input.GetMouseButton(0))
            {
                localPlayer.gameObject.transform.GetChild(3).GetChild(0).GetChild(0).gameObject.SetActive(true);
                press += Time.deltaTime * 2000;
                UIManager.Length(new Vector2(1.2f, press / 200));
            }
            if (Input.GetMouseButtonUp(0))
            {
                //FireData fireData = new global::FireData();
                //fireData.username = localPlayer.userdata.username;
                //fireData.pos = firePoint.position;
                //fireData.dir = firePoint.rotation;
                //fireData.force = press;
                //NetworkManager.Instance.SendMessage(Protocol.PlayerFire_CMD, JsonMapper.ToJson(fireData));
                localPlayer.gameObject.transform.GetChild(3).GetChild(0).GetChild(0).gameObject.SetActive(false);

                localPlayer.Fire(press);
                press = 0;
            }

            float dx = Input.GetAxis("Horizontal");
            float dy = Input.GetAxis("Vertical");

            Vector3 rot = new Vector3(0, dx, 0);
            Vector3 dir = new Vector3(0, 0, dy);
            if (dx != 0 || dy != 0)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic["username"] = localPlayer.userdata.username;
                dic["dir"] = JsonUtility.ToJson(dir);
                dic["rot"] = JsonUtility.ToJson(rot);
                NetworkManager.Instance.SendMessage(Protocol.PlayerMove_CMD, JsonMapper.ToJson(dic));
            }

            localPlayer.Move(dir);
            localPlayer.Rot(rot);
        }
    }
}

